<?php

class publicClassAction extends ActionNot
{

    public function initAction()
    {
        $this->mweblogin(0, false);
    }

    //文档预览的
    public function fileviewerAction()
    {
        $id = (int)$this->get('id', '0');
        $fobj = m('file');
        $frs = $fobj->getone($id);
        if (!$frs) exit('文件的记录不存在了1');
        $type = $frs['fileext'];
        $filepath = $frs['filepath'];

        if (substr($filepath, 0, 4) != 'http' && !file_exists($filepath)) exit('文件不存在了2');

        $types = ',' . $type . ',';
        //可读取文件预览的扩展名
        $docx = ',doc,docx,xls,xlsx,ppt,pptx,';
        if (contain($docx, $types)) {
            $filepath = $frs['pdfpath'];
            if (isempt($filepath)) {
                $this->topdfshow($frs, 1);
                return;
            }
            if (!file_exists($filepath)) {
                $this->topdfshow($frs, 1);
                return;
            } else {
                $exta = substr($filepath, -4);
                if ($exta == 'html') $this->rock->location($filepath);
            }
        } else if ($type == 'mp4') {
            $this->displayfile = '' . P . '/public/fileopen_mp4.html';
        } else if ($fobj->isyulan($type)) {
            $content = file_get_contents($filepath);
            if (substr($filepath, -6) == 'uptemp') $content = base64_decode($content);
            $bm = c('check')->getencode($content);
            if (!contain($bm, 'utf')) {
                $content = @iconv($bm, 'utf-8', $content);
            }
            $this->smartydata['content'] = $content;
            $this->displayfile = '' . P . '/public/fileopen.html';
        } else if ($type == 'pdf') {

        } else {
            $this->topdfshow($frs, 0);
            return;
            //exit('文件类型为['.$type.']，不支持在线预览');
        }
        $str = 'mode/pdfjs/web/viewer.css';
        if (!file_exists($str)) exit('未安装预览pdf插件，不能预览该文件，可到信呼官网下查看安装方法，<a target="_blank" href="' . URLY . 'view_topdf.html">查看帮助?</a>。');
        $this->smartydata['filepath'] = $this->jm->base64encode($filepath);
        $this->smartydata['filepaths'] = $filepath;
        $this->smartydata['filename'] = $frs['filename'];
        $fobj->addlogs($id, 0);//记录预览记录
    }

    private function topdfshow($frs, $lx = 0)
    {
        if ($lx == 1) {
            $officeyl = getconfig('officeyl', '0');
            if ($officeyl == '2' || $officeyl == '3') {//用微软文档服务
                $filepath = $frs['filepath'];
                if (substr($filepath, 0, 4) != 'http') {
                    $filepath = '' . getconfig('outurl', URL) . '' . $filepath . '';
                }
                $url = 'https://view.officeapps.live.com/op/view.aspx?src=' . urlencode($filepath) . '';
                if ($officeyl == '3') $url = 'https://docview.mingdao.com/op/view.aspx?src=' . urlencode($filepath) . '';
                $this->rock->location($url);
                return;
            }
        }
        $this->displayfile = '' . P . '/public/filetopdf.html';
        $this->smartydata['frs'] = $frs;
        $this->smartydata['ismobile'] = $this->rock->ismobile() ? '1' : '0';
    }

    /**
     *    请求转化
     */
    public function changetopdfAjax()
    {
        $id = (int)$this->get('id', '0');
        return c('xinhuapi')->officesend($id);
    }

    /**
     *    获取状态
     */
    public function officestatusAjax()
    {
        $id = (int)$this->get('id', '0');
        return c('xinhuapi')->officestatus($id);
    }

    /**
     *    获取状态
     */
    public function officedownAjax()
    {
        $id = (int)$this->get('id', '0');
        return c('xinhuapi')->officedown($id);
    }

    //文档预览2020.5.28
    public function filepathAction()
    {
        $id = (int)$this->get('id', '0');
        $lx = (int)$this->get('lx', '0');
        $fobj = m('file');
        $frs = $fobj->getone($id);
        if (!$frs) exit('文件的记录不存在了1');
        $type = $frs['fileext'];
        $filepath = $frs['filepath'];
        if (substr($filepath, 0, 4) != 'http' && !file_exists($filepath)) exit('文件不存在了2');
        //获取域名
        $frs['web_url'] = 'http://' . $_SERVER['SERVER_NAME'];

        //新增获取预览地址2020.5.27
        if ($type == 'doc' || $type == 'docx') {
            $frs['doctype'] = 'text';
        } else if ($type == 'ppt' || $type == 'pptx') {
            $frs['doctype'] = 'presentation';
        } else if ($type == 'xls' || $type == 'xlsx') {
            $frs['doctype'] = 'spreadsheet';
        }
        $frs['callback'] = 'index.php?m=public&a=fileeditcall&id=' . $id . '&lx=1';
        if ($lx == 1 || $lx == -1) {
            //预览
            $frs['edit_web'] = getconfig('viewpath', 'http://view.sesoffice.cn');
            //$this->displayfile = '' . P . '/public/filetopdf2.html';
            $fileurl = $frs['web_url'] . '/' . $frs['filepath'];
            //自定义签名
            $watermark = $this->getwatermark();
            $frs['watermark'] = $watermark;
            if ($lx == -1) {
                //手机
                $url = $frs['edit_web'] . '/onlinePreview?url=' . urlencode($fileurl) . $watermark;
                header("Location:$url");
                exit();
            } else {

                //判断是否是IE浏览器
                @include_once('../model/fileModel.php');
                $isie = new fileClassModel();
                if ($isie->isIE()) {
                    $frs['isie'] = 1;
                } else {
                    $frs['isie'] = 0;
                }
                //电脑
                $this->displayfile = '' . P . '/public/filetopdf2.html';
            }


        } elseif ($lx == 2) {
            //编辑
            $frs['edit_web'] = getconfig('editpath', 'http://doc.sesoffice.cn');

            $this->displayfile = '' . P . '/public/fileedit_onlyoffice.html';
        }
        $this->smartydata['frs'] = $frs;
    }

    //获取签名
    public function getwatermark()
    {
        $arr = ['watermark_txt', 'watermark_font', 'watermark_x_space', 'watermark_y_space', 'watermark_fontsize', 'watermark_color', 'watermark_width', 'watermark_height', 'watermark_alpha', 'watermark_angle'];
        $watermark = '';
        if (getconfig('watermark_txt', '') != '') {
            $watermark .= '&watermarkTxt=' . getconfig('watermark_txt', '');
        }
        if (getconfig('watermark_font', '') != '') {
            $watermark .= '&watermarkFont=' . getconfig('watermark_font', '');
        }
        if (getconfig('watermark_color', '') != '') {
            $watermark .= '&watermarkColor=' . getconfig('watermark_color', '');
        }
        if (getconfig('watermark_x_space', '') != '') {
            $watermark .= '&watermarkXspace=' . getconfig('watermark_x_space', '');
        }
        if (getconfig('watermark_y_space', '') != '') {
            $watermark .= '&watermarkYspace=' . getconfig('watermark_y_space', '');
        }
        if (getconfig('watermark_fontsize', '') != '') {
            $watermark .= '&watermarkFontsize=' . getconfig('watermark_fontsize', '');
        }

        if (getconfig('watermark_width', '') != '') {
            $watermark .= '&watermarkWidth=' . getconfig('watermark_width', '');
        }
        if (getconfig('watermark_height', '') != '') {
            $watermark .= '&watermarkHeight=' . getconfig('watermark_height', '');
        }
        if (getconfig('watermark_alpha', '') != '') {
            $watermark .= '&watermarkAlpha=' . getconfig('watermark_alpha', '');
        }
        if (getconfig('watermark_angle', '') != '') {
            $watermark .= '&watermarkAngle=' . getconfig('watermark_angle', '');
        }
        return $watermark;
    }

//文档路径的2020.5.29
    public function fileeditcallAction()
    {
        $id = (int)$this->get('id', '0');
        $fobj = m('file');
        $frs = $fobj->getone($id);
        //获取域名
        $web_url = 'http://' . $_SERVER['SERVER_NAME'];

        if (($body_stream = file_get_contents("php://input")) === FALSE) {
            echo "Bad Request";
        }
        $path_for_save = $web_url . '/' . $frs['filepath'];
        $data = json_decode($body_stream, TRUE);

        file_put_contents("test.txt", $data);

        if ($data["status"] == 2) {
            $downloadUri = $data["url"];

            if (($new_data = file_get_contents($downloadUri)) === FALSE) {
                echo "Bad Response";
            } else {
                file_put_contents($path_for_save, $new_data, LOCK_EX);
            }
        }
        echo "{\"error\":0}";
    }

    private function createtempurl($frs)
    {
        $str = 'rockdoc_' . md5(config('app.url')) . '_' . $frs->filesize . '_' . $frs->filenum . '.' . $frs->fileext . '';
        return $str;
    }


    private function saveeditback($frs, $url, $data = '', $callb = '')
    {
        $upobj = c('upfile', $this->useainfo);
        $newpath = '' . config('rock.updir') . '/' . date('Y-m') . '';
        if (!is_dir($newpath)) mkdir($newpath);
        $file_ext = '';
        $newext = $frs->fileext; //新扩展名，都会呗转为x的格式

        $randname = '' . date('d_His') . '' . rand(10, 99) . '';
        $filepath = '' . $newpath . '/' . $randname . '.' . $newext . '';
        if ($data == '') {
            if ($newext == 'xls') $newext = 'xlsx';
            if ($newext == 'doc') $newext = 'docx';
            if ($newext == 'ppt') $newext = 'pptx';
            $cont = @file_get_contents($url);
        } else {
            $cont = base64_decode($data);
        }

        if (!isempt($cont)) {
            $bo = c('base')->createtxt($filepath, $cont);
            if (!$bo) {
                c('log', $this->useainfo)->adderror('在线编辑', '在线编辑文件“' . $frs->filename . '”无法保存到服务器');
                return false;
            }
        } else {
            c('log', $this->useainfo)->adderror('在线编辑', '在线编辑文件“' . $frs->filename . '”无法获取文件');
            return false;
        }

        //备份旧的
        $oldfilename = $frs->filename;
        $oldext = $upobj->getext($oldfilename, 1);
        $dtss = date('YmdHis', strtotime($frs->adddt));
        $oldfilename = str_replace('.' . $oldext . '', '(备份' . $dtss . ').' . $oldext . '', $oldfilename);
        $upbarr = $upobj->uploadback(array(
            'filesize' => $frs->filesize,
            'filesizecn' => $frs->filesizecn,
            'fileext' => $frs->fileext,
            'filetype' => $frs->filetype,
            'allfilename' => $frs->filepath,
            'oldfilename' => $oldfilename,
        ), '', array(
            'pdfpath' => $frs->pdfpath,
            'oid' => $frs->id,
            'cid' => $this->companyid,
            'uid' => $this->userid,
            'aid' => $this->useaid,
            'optname' => $this->useainfo->name,
            'adddt' => $frs->adddt
        ));

        //更新为最新
        $filesize = filesize($filepath);
        $filename = $frs->filename;
        if ($newext != $frs->fileext) {
            $filename = str_replace('.' . $oldext . '', '.' . $newext . '', $filename);
        }
        $nuarr = array(
            'fileext' => $newext,
            'filepath' => $filepath,
            'filename' => $filename,
            'filesize' => $filesize,
            'filesizecn' => $upobj->formatsize($filesize),
            'optdt' => nowdt(),
        );
        DB::table('fileda')->where('id', $frs->id)->update($nuarr);

        if (!isempt($callb) && method_exists($this, $callb)) $this->$callb($frs->filenum, $frs); //回调处理

        return $filesize;
    }
}
